<?php
jimport('joomla.application.component.controller');
class svController extends JController
{
	function display()
	{
		if(!JRequest::getVar('view')){
			JRequest::setVar('view','home');//mac dinh vew default la view home
		}
		parent::display();
	}
	function __construct($config=array())
	{
		parent::__construct($config);
	    $this->view=JRequest::getVar('view');
		$this->model=$this->getModel($this->view);
	}
	function save()
	{
		global $option;
		$this->do_save($this->view);
		$url='index.php?option='.$option.'&view='.$this->view;
		$msg=JText::_('data saved');
		header('Location:'.$url);
	}
	function apply()
	{
		$id=JRequest::getInt('id');
		global $option;
		$this->do_save($this->view);
		$url1='index.php?option='.$option.'&view='.$this->view.'&task=edit&cid[]='.$id;
		$msg1=JText::_('data applied');
		header('Location:'.$url1);
	}
	function do_save($vi)
	{
		switch ($vi)
		{
			case 'canbo':
				$this->_saveCanbo();
				break;
			case 'useraccount':
				$this->_saveUseraccount();
				break;
			case 'trangcanhan':
				$this->_saveTrangcanhan();
				break;
			default :
				JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
				$demo=JTable::getInstance($vi);
				$demo->bind(JRequest::get('post'));
				$demo->store();
				break;
		}
		parent::display();
	}
	function cancel()
	{
		global $option;
		$url='index.php?option='.$option.'&view='.$this->view;
		header('Location:'.$url);
	}
	function remove()
	{
		global $option;
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$id     = $idlist[0];
		$this->model->remove($id);
		$url='index.php?option='.$option.'&view='.$this->view;
		header('Location:'.$url);
	}
	function _saveTrangcanhan()
	{
		$model=$this->getModel('trangcanhan');
		 $username=JRequest::getVar('username');
		 $password=JRequest::getVar('password2');
		 $repass=JRequest::getVar('password3');
		 if($password=="")
		 {
		 	$model->editAcc($password,$username);
		 }
		 else {
		 	if(strcmp($password,$repass)==0)
		 	{
		 		$model->editAcc($password,$username);
		 	}
		 }
	}
	function _saveUseraccount()
	{
		$model=$this->getModel('useraccount');
		// lay du lieu
		$ac['canbo']=JRequest::getInt('sn_idcanbo');
		$ac['quyen']=JRequest::getVar('quyen');
		$ac['name']=JRequest::getVar('namecb');
		$ac['username']=JRequest::getVar('username');
		$ac['email']=JRequest::getVar('email');
		$ac['password']=JRequest::getVar('password', '', 'post', 'string', JREQUEST_ALLOWRAW);
		$ac['password2']=JRequest::getVar('password2');
		$ac['gid']=JRequest::getInt('gid');
		$ac['block']=JRequest::getInt('block');
		$ac['sendEmail']=JRequest::getInt('sendEmail');
		$ac['dateUpdate']=JFactory::getDate()->toMysql();
		//-------------------
		$ac['idaccount']=JRequest::getInt('idaccount');
		$alloww=1;
		if($ac['canbo']==0) $alloww=0;
		if($ac['quyen']=='') $alloww=0;
		if($ac['username']=='') $alloww=0;
		if($ac['username']!=''){ if(strlen($ac['username'])<8) $alloww=0;}
		if($ac['email']=='') $alloww=0;
		if($ac['password']!=''){
			if(strlen($ac['password'])<6) $alloww=0;
		}
		if(strcmp($ac['password'],$ac['password2'])==1) $alloww=0;
		if($ac['gid']==0) $alloww=0;
		if($alloww==1)
		{
			if($ac['idaccount']=='')
			{
			// add
				$model->addAccount($ac);
			}
			else
			{
			// edit
				$model->editAccount($ac);
			}
		}
	}
	function _saveCanbo()
	{
		// lay du lieu cho tbl can bo
		$ten=JRequest::getVar('ten');
		$mieuta = JRequest::getVar('mieuta', 'Mieu ta ve phong ban', 'POST','validation type',JREQUEST_ALLOWHTML);
		$namsinh=JRequest::getVar('namsinh');
		$quequan=JRequest::getVar('quequan');
		$cmnd=JRequest::getVar('cmnd');
		$gioitinh=JRequest::getVar('gioitinh');
		$diachi=JRequest::getVar('diachi');
		$dienthoai=JRequest::getVar('dienthoai');
		$tongiao=JRequest::getVar('tongiao');
		$honnhan=JRequest::getVar('honnhan');
		$hinhanh=JRequest::getVar('hinhanh');
		$dantoc=JRequest::getVar('dantoc');
		//-------- tbl quan ly chung
		$idphongban=JRequest::getInt('idphongban');
		$idcanbo=JRequest::getInt('idcanbo');
		$idchucvu=JRequest::getInt('idchucvu');
		$idchucdanh=JRequest::getInt('idchucdanh');
		// xu ly yeu cau edit/insert
		$model=$this->getModel('canbo');
		if($idcanbo==0)// xac thuc viec them mot item
		{
			$idcanbo=$model->getMaxIDCanbo();
			$idcanbo++;
			$exec=$model->addNewCanbo($ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc);
			$exec2=$model->addNewQuanlychung($idphongban,$idchucvu,$idchucdanh,$idcanbo);
			echo $exec;
			echo $exec2;
		}
		else//xac thuc viec edit mot item
		{
			$idqlc=JRequest::getInt('idqlc');
			$exec=$model->editCanbo($idcanbo,$ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc);
			$exec2=$model->editQuanlychung($idphongban,$idchucvu,$idchucdanh,$idqlc);
		}
	}
}
?>